package com.book.web;

import com.book.domain.Student;
import com.book.service.StudentService;
import com.book.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/studentLoginServlet")
public class StudentLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.login(username, password);
        if (student == null) {
            request.setAttribute("login_Error", "用户名或密码错误，请重新输入");
            //转发到登录页面
            request.getRequestDispatcher("/stuLogin.jsp").forward(request, response);
        } else {
            //登陆成功 设置Session域参数
            session.setAttribute("student", student);
            //重定向到success.jsp
            response.sendRedirect(request.getContextPath()+"/stuFindBookServlet");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
